let { addscience ,addOperateLog} = require("../../../api/programs/add");
let { getscience } = require("../../../api/programs/Detail");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    xmfl:'',
     // 行业分类
     showHyfl: false,
     hyflOptions: [{
         code: "1",
         name: "环保",
         child: [{
             code: "01",
             name: "环境污染防治和处理设备",
           },
           {
             code: "02",
             name: "环境污染药剂和材料",
           },
           {
             code: "03",
             name: "环境监测仪器设备",
           },
           {
             code: "04",
             name: "环保工程勘察设计与施工",
           },
           {
             code: "05",
             name: "环境评估与检测服务",
           },
           {
             code: "06",
             name: "生态环境保护及污染治理服务",
           },
           {
             code: "07",
             name: "环保技术研发与技术服务",
           },
           {
             code: "08",
             name: "其他环保",
           },
         ],
       },
       {
         code: "2",
         name: "新能源",
         child: [{
             code: "09",
             name: "太阳能",
           },
           {
             code: "10",
             name: "风能",
           },
           {
             code: "11",
             name: "氢能",
           },
           {
             code: "12",
             name: "储能",
           },
           {
             code: "13",
             name: "智能电网",
           },
           {
             code: "14",
             name: "其他新能源",
           },
 
         ],
       },
       {
         code: "3",
         name: "大健康",
         child: [{
             code: "15",
             name: "生物技术",
           },
           {
             code: "16",
             name: "体外诊断",
           },
           {
             code: "17",
             name: "医疗机器人",
           },
           {
             code: "18",
             name: "康复辅具",
           },
           {
             code: "19",
             name: "植介入器械",
           },
           {
             code: "20",
             name: "低值耗材",
           },
           {
             code: "21",
             name: "医疗设备",
           },
           {
             code: "22",
             name: "医疗器械销售",
           },
           {
             code: "23",
             name: "其他大健康",
           }
         ],
       },
       {
         code: "4",
         name: "其他产业",
         child: [{
           code: "24",
           name: "其他产业",
         }],
       },
     ],
 
     fieldNames: {
       text: "name",
       value: "code",
       children: "child",
     },
     hyflValue: "",
     cascaderHyfl: "",
    modelType:["创业团队","注册企业"],
    ModelShow:false,
    xmbh: "",
    minDate: new Date(1970, 1, 1).getTime(),
    maxDate: new Date().getTime(),
    science: {
      xmbh: "", //项目编号
      ztms: "创业团队", //主体模式
      rcname: '',
      byyx: '',
      byzy:'',
      byxl: '',
      bysj: '',
      zyjs: '', 
      mqzz:'',
      zcjs:'',
      lssj: '',
      hyfla: '',
      hyflb: '',
      cycs: '',
      xmzscq:'',
      qysbrs: '',
      tname:'',
    }, //信息

    showDialog: false,

    fieldNames: {
      text: "name",
      value: "code",
      children: "child",
    },

    // 时间
    showyjldsj: false,
    showbysj: false,
    showlssj: false,
    currentyjldsj: "",
    currentbysj: "",
    currentlssj: "",
    formatter(type, value) {
      if (type === "year") {
        return `${value}年`;
      }
      if (type === "month") {
        return `${value}月`;
      }
      return value;
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      "science.xmbh": options.xmbh,
      "science.tname": options.tname,
      bm:options.bm,
      tname:options.tname,
      xmmc:options.xmmc,
      xmfl:options.xmfl
    });
    (options.kzID !== null) &
    (options.kzID !== "") &
    (options.kzID !== undefined)
      ? this.getscience()
      : "";
  },
  
  onClickHyfl(e) {
    this.setData({
      showHyfl: true,
    });
  },

  onChangeHyfl(e) {},
  onFinishHyfl(e) {
    const {
      selectedOptions,
      value
    } = e.detail;

    const hyflValue = selectedOptions
      .map((option) => option.text || option.name)
      .join("/");

    this.setData({
      hyflValue,
      cascaderHyfl: value,
      "science.hyfla": selectedOptions[0].name,
      "science.hyflb": selectedOptions[1].name,
    });
    console.log(this.data.science.hyfla)
    this.onClose()
  },
  clickModel(){
    this.setData({
      ModelShow:true
    })
  },
  closeModel(){
    this.setData({
      ModelShow:false
    })
  },
  getscience() {
    getscience(this.data.science.xmbh).then((res) => {
      let newData = { ...this.data.science };
      this.setData({
        science: { ...newData, ...res.data[0] },
      });
      if (
        this.data.science.hyflb != ''
      ) {
        let v;

        this.data.hyflOptions.forEach((item) => {
          return item.child.map((value) => {
            if (value.name == this.data.science.hyflb ) {
              console.log(value.name ,value.code, this.data.science.hyflb ,res.data[0].hyflb)
              v = value;
              return value;
            }
          });
        });
        console.log(v)
        this.setData({
          cascaderHyfl: v.code,
          hyflValue: `${this.data.science.hyfla}/${this.data.science.hyflb}`,
        
        });
      }
    });
  },
  onPush() {
    try {
      let JyText 
      if(this.data.xmfl==='人才项目'||this.data.xmfl==='测试项目'){
        JyText = {
          rcname: '',
          byyx: '',
          byxl: '',
          bysj: '',
          zyjs: '', 
          zyyw:'',
          zycp:'',
          hyfla: '',
          hyflb: '',
        }
        console.log('gg')
      }else  if(this.data.xmfl==='科技项目'||this.data.xmfl==='测试项目'){
        JyText = {
          lssj: '',
          // hyfla: '',
          // hyflb: '',
          cycs: '',
          qysbrs: ''
        }
      }
     
      for (const key in JyText) {
        if (this.data.science[key] === "") {
          console.log(key);
          wx.showToast({
            title: "请填写必填项",
            icon: "error",
            duration: 2000, //持续的时间
          });
          return;
        }
      }
      console.log('；',this.data.science)

      this.addscience();
    } catch (error) {
      console.log(error);
    }
  },
  addscience() {
    let time = new Date().getTime();
    //将时间戳格式转换成年月日时分秒
    let date = new Date(time);
    let bbTime =`${date.getFullYear()}-${
      date.getMonth() + 1 < 10
        ? "0" + (date.getMonth() + 1)
        : date.getMonth() + 1
    }-${date.getDate() < 10 ? "0" + date.getDate() : date.getDate()}`;
    let tjTime = `${
      date.getHours() < 10 ? "0" + date.getHours() : date.getHours()
    }:${date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()}:${
      date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds()
    }`;
   if (!this.data.science.kzID) {
    this.setData({
      "science.tjsj": `${bbTime} ${tjTime}`,
    })
      addOperateLog({
        type: 2,
        khbh: '',
        xmbh: this.data.science.xmbh,
        xmmc: this.data.xmmc,
        bm: this.data.bm,
        tname: this.data.tname,
        tjsj: `${bbTime} ${tjTime}`,
      });
    
   }
   for (const key in this.data.science) {
    if (this.data.science[key] === null||this.data.science[key] === 'null') {
      let param = `science.${key}`
      this.setData({
        [param]: ''
      })

    }
  }
   console.log('add',this.data.science)
    addscience(this.data.science).then((res) => {
      wx.showToast({
        title: "人才信息填报完成",
        icon: "none",
      });
      setTimeout(() => {
        this.setData({ showDialog: true });
        let pages = getCurrentPages();
        let prevPage = pages[pages.length - 2];
                 wx.navigateBack({
                   delta: -1,
                   success: function (e) { // 成功的回调
                     if (prevPage == undefined || prevPage  == null) return;
                     prevPage .getDetail();  // 调用A页面的方法, 并将值传过去
                   }
                 });
      }, 1000);
    });
  },
  onInputTime(e) {
    let param = e.currentTarget.dataset.name; //字段名
    let value = e.detail; //值

    let key = `current${param}`;
    this.setData({
      [key]: value,
    });
    console.log(key, this.data[key]);
  },

  inputValue(e) {
    let param = e.currentTarget.dataset.name; //字段名
    let value = e.detail; //值
    if (param == 'ztms') {
      value = value.value
    }
    let key = `science.${param}`;
    // const msg=`checkData.${param}`;

    this.setData({
      [key]: value,
      // [msg]:'' //清空错误提示
    });
  },
  // 格式化时间
  formatterTime(time) {
    let date = new Date(time);
    let year = date.getFullYear();
    console.log(time,date,year)
    let month = ("0" + (date.getMonth() + 1)).slice(-2);
    let day = ("0" + date.getDate()).slice(-2);
    let hours = ("0" + date.getHours()).slice(-2);
    let minutes = ("0" + date.getMinutes()).slice(-2);
    let seconds = ("0" + date.getSeconds()).slice(-2);
    let formattedTime = year + "-" + month + "-" + day;
    return formattedTime;
  },
  onconfirmTime(e) {
    let param = e.currentTarget.dataset.name; //字段名
    console.log(param)
    let Time = this.formatterTime(this.data[`current${param}`]);
    let key = `science.${param}`;

    this.setData({
      [key]: Time,
    });
    this.setData({
      showyjldsj: false,
      showbysj: false,
      showlssj: false,
      
    });
  },
  onconfirm() {
    this.setData({
      showyjldsj: false,
    });
  },
  // onChange(event) {
  //   this.setData({
  //     "science.rzxt": event.detail,
  //   });
  // },
  onClickyjldsj(e) {
    this.setData({
      showyjldsj: true,
    });
  },
  onClickbysj(e) {
    this.setData({
      showbysj: true,
    });
  },
  onClicklssj(e) {
    this.setData({
      showlssj: true,
    });
  },
  onClose() {
    this.setData({
      showHyfl: false,
      ModelShow:false,
      showyjldsj: false,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
